#include "clickableLabel.h"
#include <QEvent>
#include <QMouseEvent>

bool MovableLabel::eventFilter(QObject *obj, QEvent *event){
    if(event->type() == QEvent::MouseButtonPress){
        QMouseEvent *ev = dynamic_cast<QMouseEvent*>(event);
        if(ev != NULL){
            if(ev->button() == Qt::LeftButton){
                unSelect();
                clicked = false;
            } else if(ev->button() == Qt::RightButton){
                if(object->getSelected()){
                    Unit *unt = dynamic_cast<Unit*>(object);
                    if(unt != NULL){
                        unt->stopAttacking();
                        object->setTarget(ev->pos().x(), ev->pos().y());
                    }
                }
            }
        }
    }
    return false;
}
MovableLabel::MovableLabel(QWidget *parent, Entity *obj, QString img, QString selImg):
    QLabel(parent), win(parent), object(obj), imageNorth(":/images/" + img + "_N"), SelImageNorth(":/images/" + selImg + "_N"),image_south(":/images/" + img + "_S"),
    imageEast(":/images/" + img + "_E"), imageWest(":/images/" + img + "_W"), SelImageSouth(":/images/" + selImg + "_S"), SelImageEast(":/images/" + selImg + "_E"),
    SelImageWest(":/images/" + selImg + "_W")
{
    setMouseTracking(true); // enable mouse move events
    setPixmap(imageNorth);
    setGeometry(0,0,imageNorth.width(), imageNorth.height());
}
MovableLabel::~MovableLabel(){
    delete object;
}

void MovableLabel::unSelect(){
    object->setSelected(false);
    clicked = false;
    //setPixmap(imageNorth);
    this->updateImgae();
}
void MovableLabel::updateImgae(){
    //if(object->getDir() != dir){
        if(!object->getSelected()){
            if(object->getDir() == "N"){
                setPixmap(imageNorth);
                setGeometry(object->getX(),object->getY(),imageNorth.width(), imageNorth.height());
            } else if(object->getDir() == "S"){
                setPixmap(image_south);
                setGeometry(object->getX(),object->getY(), image_south.width(), image_south.height());
            } else if(object->getDir() == "E"){
                setPixmap(imageEast);
                setGeometry(object->getX(),object->getY(), imageEast.width(), imageEast.height());
            } else if(object->getDir() == "W"){
                setPixmap(imageWest);
                setGeometry(object->getX(),object->getY(), imageWest.width(), imageWest.height());
            }
            dir = object->getDir();
        } else {
            if(object->getDir() == "N"){
                setPixmap(SelImageNorth);
                setGeometry(object->getX(),object->getY(),SelImageNorth.width(), SelImageNorth.height());
            } else if(object->getDir() == "S"){
                setPixmap(SelImageSouth);
                setGeometry(object->getX(),object->getY(), SelImageSouth.width(), SelImageSouth.height());
            } else if(object->getDir() == "E"){
                setPixmap(SelImageEast);
                setGeometry(object->getX(),object->getY(), SelImageEast.width(), SelImageEast.height());
            } else if(object->getDir() == "W"){
                setPixmap(SelImageWest);
                setGeometry(object->getX(),object->getY(), SelImageWest.width(), SelImageWest.height());
            }
            dir = object->getDir();
        }
    //}
}


